// pages/orders/index/index.js
import http from '../../../services/http/index'
import { ListData } from '../../../utils/Data'
let util = require('../../../utils/index')

let allTabImgs = ['../../../images/orders/airportshuttle.png','../../../images/orders/airportshuttle_ac.png','../../../images/orders/travel.png','../../../images/orders/travel_ac.png','../../../images/orders/bus.png','../../../images/orders/bus_ac.png','../../../images/orders/reservation.png','../../../images/orders/reservation_ac.png']


Page({

  /**
   * 页面的初始数据
   */
  data: {
    maskFlag: true,
    cindex:0,
    tabImgs:[{label:'机场接送',type:0,img:allTabImgs[1]},
      { label:'旅游包车',type:1,img:allTabImgs[2]},
      {label:'结伴班车',type:2,img:allTabImgs[4]},
      {label:'预约用车',type:3,img:allTabImgs[6]}],

    itemMissionList: {
      data:[],
      hasMore:true,
      page:1
    }, //"机场接送"抢单列表
    itemCharter: {
      data:[],
      hasMore:true,
      page:1
    },//“包车”抢单列表
    itemBusList: {
      data:[],
      hasMore:true,
      page:1
    }, //"班车"抢单列表
    itemAppointCar: {
      data:[],
      hasMore:true,
      page:1
    },//“预约用车”抢单列表

    kejiedanCar:{},//可接单车辆
    id: '',//抢单接送机id
    price: '',//可接单车辆 弹窗 价格
    robbingType:'',//抢单类型
    car_id: '',//接单车辆id
    //弹窗
    alert: {
      show: false,
      type: 1,
      color:'green',
      img:'../../../images/icon_ok_big@2x.png',
      title: '抢单成功',
      text1: '恭喜你已抢单成功，请及时',
      text2: '到达指定地点接送乘客哦~',
      btn1:'继续抢单',
      btn2: '查看订单',
      display:'block',
      closeModelType:'', //点击函数
      closeModelType2:'' //点击函数-查看详情
    }
  },


  //tab切换
  chooseType: function (e) {
    var type = e.currentTarget.dataset.type;
    var tabImgs = this.data.tabImgs;
    var initIndex = [0,2,4,6];
    initIndex[type]++;
    tabImgs.forEach(function(el,i){
      el.img = allTabImgs[initIndex[i]]
    })
    this.setData({
      tabImgs:tabImgs,
      cindex: type
    });
    if (type == 0) {
      // 刷新"机场接送"抢单列表
      this.setData({
        itemMissionList: {
          data:[],
          hasMore:true,
          page:1
        }
      });
      this.getMissionInfo(); 
    } else if (type == 1) {
      // 刷新"包车"抢单列表
      this.setData({
        itemCharter: {
          data:[],
          hasMore:true,
          page:1
        }
      });
      this.getCharterInfo(); 
    } else if (type == 2) {
      // 刷新"班车"抢单列表
      this.setData({
        itemBusList: {
          data:[],
          hasMore:true,
          page:1
        }
      });
      this.getBusInfo(); 
    } else if (type == 3) {
      // 刷新"预约用车"抢单列表
      this.setData({
        itemAppointCar: {
          data:[],
          hasMore:true,
          page:1
        }
      });
      this.getAppointCarInfo();
    }
  },

  // 跳转详情
  navagater:function(e){
    console.log(e)
    var cIndex=e.currentTarget.dataset.cindex;
    var missProId = e.currentTarget.dataset.id;
    util.default.go(`../detail/detail?type=${cIndex}&orderId=${missProId}`);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // 刷新"机场接送"抢单列表
    this.getMissionInfo();
  },

  // 请求-"机场接送"抢单列表
  getMissionInfo() {
    if(!this.data.itemMissionList.hasMore)return false;
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    var param = {
      page:this.data.itemMissionList.page
    };
    http.getMission(param)
      .then(data => {
        var hasMore = false,page=param.page;
        if(data.next_page){
          //有下一页
          hasMore = true;
          page++;
        }else{
          hasMore = false
        };
        var oldData = this.data.itemMissionList.data;
        var newData = oldData.concat(data.mission);
        var itemMissionListData = Object.assign(new ListData(), {
          data: newData,
          hasMore:hasMore,
          page:page
        });
        that.setData({
          itemMissionList: itemMissionListData
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },

  // 请求-"包车"抢单列表
  getCharterInfo() {
    if(!this.data.itemCharter.hasMore)return false;
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    var param = {
      page:this.data.itemCharter.page
    };
    http.getCharter(param)
      .then(data => {
        var hasMore = false,page=param.page;
        if(data.next_page){
          //有下一页
          hasMore = true;
          page++;
        }else{
          hasMore = false
        };
        var oldData = this.data.itemCharter.data;
        var newData = oldData.concat(data.list);
        var itemCharterData = Object.assign(new ListData(), {
          data: newData,
          hasMore:hasMore,
          page:page
        })
        that.setData({
          itemCharter: itemCharterData
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },


  // 请求-"班车"抢单列表
  getBusInfo() {
    if(!this.data.itemBusList.hasMore)return false;
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    var param = {
      page:this.data.itemBusList.page
    };
    http.getBus(param)
      .then(data => {
        var hasMore = false,page=param.page;
        if(data.next_page){
          //有下一页
          hasMore = true;
          page++;
        }else{
          hasMore = false
        };
        var oldData = this.data.itemBusList.data;
        var newData = oldData.concat(data.list);
        var itemBusListData = Object.assign(new ListData(), {
          data: newData,
          hasMore:hasMore,
          page:page
        });
        that.setData({
          itemBusList: itemBusListData
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },
  
  // 请求-"预约用车"抢单列表
  getAppointCarInfo() {
    if(!this.data.itemAppointCar.hasMore)return false;
    var that = this;
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000
    });
    var param = {
      page:this.data.itemAppointCar.page
    };
    http.getAppointCar(param)
      .then(data => {
        console.log('预约用车------------');
        console.log(data);
        console.log('预约用车-----------');
        var hasMore = false,page=param.page;
        if(data.next_page){
          //有下一页
          hasMore = true;
          page++;
        }else{
          hasMore = false
        };
        var oldData = this.data.itemAppointCar.data;
        var newData = oldData.concat(data.list);
        var itemAppointCarData = Object.assign(new ListData(), {
          data: newData,
          hasMore:hasMore,
          page:page
        })
        that.setData({
          itemAppointCar: itemAppointCarData
        });
        wx.hideToast();
      }).catch(err => {
        wx.hideToast();
        console.log(err)
      })
  },

  // 关闭遮罩层
  // showFlag: function () {
  //   this.setData({
  //     maskFlag: false,
  //     oilchooseFlag: true
  //   })
  // },

  // 关闭“选择可接单”弹框
  closeSelectModel:function(){
    this.setData({
      maskFlag: true
    })
  },

 // 点击抢单按钮 接机单：选择可接单车辆
  SelectMission(e){
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var robbingType = e.currentTarget.dataset.type;
    
    that.setData({
      id: id,
      price: price,
      robbingType: robbingType
    });

    http.getSelectMission({ id: id }).then(data => {
      wx.hideToast();
      var kejiedanCarData = Object.assign(new ListData(), {
        data: data.list
      })
      that.setData({
        kejiedanCar: kejiedanCarData.data,
      });
      that.setData({
        maskFlag: false,
        // kejiedanCar: kejiedanCar
      })
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
    })
  },

  // 点击抢单按钮 包车：选择可接单车辆
  Selectcharter(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var robbingType = e.currentTarget.dataset.type;

    that.setData({
      id: id,
      price: price,
      robbingType: robbingType
    });

    http.getSelectcharter({ id: id }).then(data => {
      wx.hideToast();
      var kejiedanCarData = Object.assign(new ListData(), {
        data: data.list
      })
      that.setData({
        kejiedanCar: kejiedanCarData.data,
      });
      that.setData({
        maskFlag: false,
        // kejiedanCar: kejiedanCar
      })
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
    })
  },

  // 点击抢单按钮 班车：选择可接单车辆
  SelectBusInfo(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var robbingType = e.currentTarget.dataset.type;

    that.setData({
      id: id,
      price: price,
      robbingType: robbingType
    });

    http.getSelectBus({ id: id }).then(data => {
      wx.hideToast();
      var kejiedanCarData = Object.assign(new ListData(), {
        data: data.list
      })
      that.setData({
        kejiedanCar: kejiedanCarData.data,
      });
      that.setData({
        maskFlag: false,
        // kejiedanCar: kejiedanCar
      })
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
    })
  },

  // 点击抢单按钮 预约用车：选择可接单车辆
  SelectCar(e) {
    wx.showToast({
      title: 'loading',
      icon: 'loading',
      duration: 10000,
      mask: true
    });
    var that = this;
    var id = e.currentTarget.dataset.id;
    var price = e.currentTarget.dataset.price;
    var robbingType = e.currentTarget.dataset.type;

    that.setData({
      id: id,
      price: price,
      robbingType: robbingType
    });

    http.getSelectAppointCar({ id: id }).then(data => {
      wx.hideToast();
      var kejiedanCarData = Object.assign(new ListData(), {
        data: data.list
      })
      that.setData({
        kejiedanCar: kejiedanCarData.data,
      });
      that.setData({
        maskFlag: false,
        // kejiedanCar: kejiedanCar
      })
    }).catch(function (err) {
      console.log(err)
      wx.hideToast();
    })
  },

  // 点击可接单车辆
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      car_id: e.detail.value
    })
  },



  // 抢单成功
  robbingSucces() {
    var that = this;
    that.setData({
      alert: {
        show: true,
        type: 1,
        color: 'green',
        img: '../../../images/icon_ok_big@2x.png',
        title: '抢单成功',
        text1: '恭喜你已抢单成功，请及时',
        text2: '到达指定地点接送乘客哦~',
        btn1: '继续抢单',
        btn2: '查看订单',
        display: 'block',
        closeModelType: 'zhijieClose', //点击函数
        closeModelType2: 'navagater'//点击函数-查看详情
      },
    })
    // setTimeout(function () {
    //   //要延时执行的代码
    //   wx.switchTab({
    //     url: '../../orderManage/orderManage'
    //   })
    // }, 2000) //延迟时间

    // wx.showToast({
    //   title: '抢单成功',
    //   icon: 'succes',
    //   duration: 5000,
    //   success: function () {
    //     setTimeout(function () {
    //       //要延时执行的代码
    //       wx.switchTab({
    //         url: '../../orderManage/orderManage'
    //       })
    //     }, 2000) //延迟时间
    //   }
    // })
  },

  // 抢单失败
  robbingFail(err) {
    var that = this;
    // 抢单失败
    that.setData({
      alert: {
        show: true,
        type: 1,
        color: 'red',
        img: '../../../images/alert-error@2x.png',
        title: '抢单失败!',
        text1: err,
        text2: '请再接再厉哦~',
        btn1: '重新抢单',
        btn2: '',
        display: 'none',
        closeModelType: 'zhijieClose'
      }
    })
  },

  // 进行抢单
  robbingCar(e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var robbingType = e.currentTarget.dataset.type;
    var car_id = e.currentTarget.dataset.car_id;
    if (!car_id){
      wx.showToast({
        title: '请先选择车辆',
        icon: 'none',
        duration: 1000,
        mask: true
      })
    }else{
      wx.showToast({
        title: 'loading',
        icon: 'loading',
        duration: 10000,
        mask: true
      });
      // 接机单
      if (robbingType==0){
        http.getRobbingMission({ id: id, car_id: car_id }).then(data => {
          console.log(data)
          wx.hideToast();
          // 刷新"机场接送"抢单列表
          that.getMissionInfo();
          // "机场接送"抢单成功
          that.robbingSucces();
        }).catch(function (err) {
          console.log(err)
          wx.hideToast();
          if (err.payload.status == 2) {
            wx.showModal({
              title: '',
              content: err.message,
              success: function (res) {
                if (res.confirm) {
                  http.getRobbingMission({ id: id, car_id: car_id, is_confirm: 1 }).then(data => {
                    // 刷新"机场接送"抢单列表
                    that.getMissionInfo();
                    // "机场接送"抢单成功
                    that.robbingSucces();
                  }).catch(function (err) {
                    that.robbingFail(err.message);
                    wx.hideToast();
                  })
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            that.robbingFail(err.message);
          }
        })
      // 包车
      } else if (robbingType == 1) {
        http.getRobbingcharter({ id: id, car_id: car_id }).then(data => {
          console.log(data)
          wx.hideToast();
          // 刷新"包车"抢单列表
          that.getCharterInfo();
          // "包车"抢单成功
          that.robbingSucces();
        }).catch(function (err) {
          console.log(err)
          wx.hideToast();
          if (err.payload.status == 2) {
            wx.showModal({
              title: '',
              content: err.message,
              success: function (res) {
                if (res.confirm) {
                  http.getRobbingcharter({ id: id, car_id: car_id, is_confirm: 1 }).then(data => {
                    // 刷新"包车"抢单列表
                    that.getCharterInfo();
                    // "包车"抢单成功
                    that.robbingSucces();
                  }).catch(function (err) {
                    that.robbingFail(err.message);
                    wx.hideToast();
                  })
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            // 抢单失败
            that.robbingFail(err.message);
          }

        })
      // 班车
      } else if (robbingType == 2) {
        http.getRobbingBus({ id: id, car_id: car_id }).then(data => {
          console.log(data)
          wx.hideToast();
          // 刷新"班车"抢单列表
          that.getBusInfo();
          // "班车"抢单成功
          that.robbingSucces();
        }).catch(function (err) {
          console.log(err)
          wx.hideToast();
          if (err.payload.status == 2) {
            wx.showModal({
              title: '',
              content: err.message,
              success: function (res) {
                if (res.confirm) {
                  http.getRobbingBus({ id: id, car_id: car_id, is_confirm: 1 }).then(data => {
                    // 刷新"班车"抢单列表
                    that.getBusInfo();
                    // "班车"抢单成功
                    that.robbingSucces();
                    wx.hideToast();
                  }).catch(function (err) {
                    that.robbingFail(err.message);
                    wx.hideToast();
                  })
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            // 抢单失败
            that.robbingFail(err.message);
          }
        })

      // 预约用车
      } else if (robbingType == 3) {
        http.appointCarGrap({ id: id, car_id: car_id }).then(data => {
          console.log(data)
          wx.hideToast();
          // 刷新"预约用车"抢单列表
          that.getAppointCarInfo();
          // "预约用车"抢单成功
          that.robbingSucces();
        }).catch(function (err) {
          console.log(err)
          wx.hideToast();
          if (err.payload.status == 2) {
            wx.showModal({
              title: '',
              content: err.message,
              success: function (res) {
                if (res.confirm) {
                  http.appointCarGrap({ id: id, car_id: car_id, is_confirm: 1 }).then(data => {
                    // 刷新"预约用车"抢单列表
                    that.getAppointCarInfo();
                    // "预约用车"抢单成功
                    that.robbingSucces();
                  }).catch(function (err) {
                    that.robbingFail(err.message);
                    wx.hideToast();
                  })
                } else {
                  console.log('用户点击取消')
                }
              }
            })
          } else {
            // 抢单失败
            that.robbingFail(err.message);
          }
        })
      }
      
    }

  },

  // 点击抢单成功和抢单失败的 “继续抢单”和“重新抢单”
  zhijieClose(e) {
    this.setData({
      alert: {
        show: false,
      }
    })
    // 刷新"机场接送"抢单列表
    this.getMissionInfo();
    // 刷新"包车"抢单列表
    this.getCharterInfo();
    // 刷新"班车"抢单列表
    this.getBusInfo();
    // 刷新"预约用车"抢单列表
    this.getAppointCarInfo();

  },

  // 请求-抢单 接机单
  RobbingMission(e) {
    // wx.showToast({
    //   title: 'loading',
    //   icon: 'loading',
    //   duration: 10000,
    //   mask: true
    // });
    // var that = this;
    // var idRobbing = e.currentTarget.dataset.id;
    // console.log("idRobbing是" + idRobbing)
    
  },

  // 请求-抢单 包车
  Robbingcharter(e) {
    // wx.showToast({
    //   title: 'loading',
    //   icon: 'loading',
    //   duration: 10000,
    //   mask: true
    // });
    // var that = this;
    // var idRobbing = e.currentTarget.dataset.id;
    // console.log("idRobbing是" + idRobbing)
    
  },

  // 请求-"班车"抢单列表
  RobbingBus(e) {
    // wx.showToast({
    //   title: 'loading',
    //   icon: 'loading',
    //   duration: 10000
    // });
    // var that = this;
    // var idRobbing = e.currentTarget.dataset.id;
    // console.log("idRobbing是" + idRobbing)
    
  },

  // 请求-抢单 预约用车
  CarMission(e) {
    // wx.showToast({
    //   title: 'loading',
    //   icon: 'loading',
    //   duration: 10000,
    //   mask: true
    // });
    // var that = this;
    // var idRobbing = e.currentTarget.dataset.id;
    // console.log("idRobbing是" + idRobbing)
    
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var type = this.data.cindex
    if (type == 0) {
      this.getMissionInfo();
    } else if (type == 1) {
      this.getCharterInfo();
    } else if (type == 2) {
      this.getBusInfo();
    } else if (type == 3) {
      this.getAppointCarInfo();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})